"use strict";
const path = require("path");
const expect = require("chai").expect;
const rm = require("rimraf-promise");
const DirectoryGenerator = require(path.join(__dirname,
                                             "..",
                                             "..",
                                             "src",
                                             "generators",
                                             "DirectoryGenerator.js"));
const sandboxPath = path.join(__dirname, "..", "fixtures", "tmp");

/** @test {DirectoryGenerator} */
describe("DirectoryGenerator", function() {
  let subject = new DirectoryGenerator();

  /** @test {DirectoryGenerator#constructor} */
  it("#constructor", function() {
    expect(subject).to.be.instanceof(DirectoryGenerator);
  });

  /** @test {DirectoryGenerator#findOrCreate} */
  describe("#findOrCreate", function() {
    describe("{name}", function(done) {
      it("should find or create a folder with a given name", function() {
        let result;
        let payload;
        payload = {
          name: path.join(sandboxPath, "app", "controller")
        };
        result = subject.findOrCreate(payload)
          .then(function(result) {
            if (true === result.hasError) {
              console.error(result);
            }
            expect(result.hasError).to.equal(false);
            done();
          });
      });
    });
    describe("{structure}", function() {
      after(function() {
        rm(path.join(sandboxPath, "models"));
        rm(path.join(sandboxPath, "views"));
        rm(path.join(sandboxPath, "controllers"));
      });
      it("should create a complete structure", function(done) {
        let payload = {
          structure: [
            path.join(sandboxPath, "models"),
            path.join(sandboxPath, "views"),
            path.join(sandboxPath, "controllers")
          ]
        };
        subject.findOrCreate(payload)
          .then(function(result) {
            if(true === result.hasError) {
              console.error(result);
            }
            expect(result.hasError).to.equal(false);
            done();
          });
      });
    });
  });
});
